package yhao.infra.web;

import org.springframework.boot.context.properties.ConfigurationProperties;
import yhao.infra.web.filter.InnerAccessCheckFilter;

import java.io.Serializable;

/**
 * @author yoara
 * <p> Created on 2019/8/12 </p>
 */
@ConfigurationProperties(prefix = YhaoInfraWebProperties.PREFIX)
public class YhaoInfraWebProperties {
    public static final String PREFIX = "yhao.infra.web";

    public static final String SESSION_EXPIRE_TIME = PREFIX+".session-filter.expire-time";
    public static final String ENCRYPT_CACHE_BEAN_NAME = PREFIX+".env.encrypt-cache-bean-name";
    public static final String WEB_LOG_PATH = PREFIX+".log-path";

    /**访问日志**/
    private String logPath;
    /**session相关设置**/
    private Session sessionFilter = new Session();

    /**阈值控制相关设置**/
    private Access accessFilter = new Access();

    /**web环境相关设置**/
    private Env env = new Env();

    /**form验证组件相关设置**/
    private FormValidation formValidation = new FormValidation();

    public FormValidation getFormValidation() {
        return formValidation;
    }

    public void setFormValidation(FormValidation formValidation) {
        this.formValidation = formValidation;
    }

    public Session getSessionFilter() {
        return sessionFilter;
    }

    public void setSessionFilter(Session sessionFilter) {
        this.sessionFilter = sessionFilter;
    }

    public Access getAccessFilter() {
        return accessFilter;
    }

    public void setAccessFilter(Access accessFilter) {
        this.accessFilter = accessFilter;
    }

    public Env getEnv() {
        return env;
    }

    public void setEnv(Env env) {
        this.env = env;
    }

    public static class FormValidation {
        /**验证组件使用缓存池（建议false）**/
        private boolean usePool = false;

        /**缓存池MaxIdle**/
        private int poolMaxIdle = 1;

        /**缓存池MaxTotal**/
        private int poolMaxTotal = 5;

        public boolean isUsePool() {
            return usePool;
        }

        public void setUsePool(boolean usePool) {
            this.usePool = usePool;
        }

        public int getPoolMaxIdle() {
            return poolMaxIdle;
        }

        public void setPoolMaxIdle(int poolMaxIdle) {
            this.poolMaxIdle = poolMaxIdle;
        }

        public int getPoolMaxTotal() {
            return poolMaxTotal;
        }

        public void setPoolMaxTotal(int poolMaxTotal) {
            this.poolMaxTotal = poolMaxTotal;
        }
    }

    public static class Env {
        /**加密组件所用的cache的BeanName，不配置则使用系统cache**/
        private String encryptCacheBeanName;

        /**是否限制跨域请求**/
        private boolean forbidCrossOrigin = true;

        /**是否检查微服务调用的ip**/
        private boolean innerAccessCheck = false;

        /**允许通过的微服务调用的ip前缀**/
        private String innerAccessIpPrefix = InnerAccessCheckFilter.DEFAULT_IP_PREFIX;

        public String getEncryptCacheBeanName() {
            return encryptCacheBeanName;
        }

        public void setEncryptCacheBeanName(String encryptCacheBeanName) {
            this.encryptCacheBeanName = encryptCacheBeanName;
        }

        public boolean isForbidCrossOrigin() {
            return forbidCrossOrigin;
        }

        public void setForbidCrossOrigin(boolean forbidCrossOrigin) {
            this.forbidCrossOrigin = forbidCrossOrigin;
        }

        public boolean isInnerAccessCheck() {
            return innerAccessCheck;
        }

        public void setInnerAccessCheck(boolean innerAccessCheck) {
            this.innerAccessCheck = innerAccessCheck;
        }

        public String getInnerAccessIpPrefix() {
            return innerAccessIpPrefix;
        }

        public void setInnerAccessIpPrefix(String innerAccessIpPrefix) {
            this.innerAccessIpPrefix = innerAccessIpPrefix;
        }
    }

    public static class Access {
        /**访问阈值控制url，多个用','分割**/
        private String urlPatterns = "/*";

        /**访问阈值控制的间隔时间，单位为秒，默认1秒**/
        private int throttlingInterval = 1;

        /**单一session阈值时间内可访问次数，默认5**/
        private int maxAccessCountPerSession = 5;

        /**单一ip阈值时间内可访问次数，默认20**/
        private int maxAccessCountPerIp = 20;

        /**阈值限制的白名单url，多个用','隔开**/
        private String bypassResources;

        public int getThrottlingInterval() {
            return throttlingInterval;
        }

        public void setThrottlingInterval(int throttlingInterval) {
            this.throttlingInterval = throttlingInterval;
        }

        public int getMaxAccessCountPerSession() {
            return maxAccessCountPerSession;
        }

        public void setMaxAccessCountPerSession(int maxAccessCountPerSession) {
            this.maxAccessCountPerSession = maxAccessCountPerSession;
        }

        public int getMaxAccessCountPerIp() {
            return maxAccessCountPerIp;
        }

        public void setMaxAccessCountPerIp(int maxAccessCountPerIp) {
            this.maxAccessCountPerIp = maxAccessCountPerIp;
        }

        public String getBypassResources() {
            return bypassResources;
        }

        public void setBypassResources(String bypassResources) {
            this.bypassResources = bypassResources;
        }

        public String getUrlPatterns() {
            return urlPatterns;
        }

        public void setUrlPatterns(String urlPatterns) {
            this.urlPatterns = urlPatterns;
        }
    }

    public static class Session implements Serializable {
        /**匹配前缀**/
        private String urlPatterns = "/*";

        /**cookie取sessionId，sessionId名称**/
        private String cookieSidName = "sid";

        /**cookie取sessionId的域名**/
        private String cookieDomain = "";

        /**cookie取sessionId的路径**/
        private String cookiePath = "/";

        /**通过request Header获取sessionId，定义获取键**/
        private String sidHeaderKey = "";

        /**通过request Header获取sessionSource，定义获取来源**/
        private String sessionSourceHeaderKey = "";

        /**redisSession的有效时间，单位为秒**/
        private int expireTime = 1800;

        /**不适用session的过滤url前缀，多个用,隔开。**/
        private String passPathPrefix;

        /**不适用session的过滤url后缀，多个用,隔开。**/
        private String passPathPostfix;

        /**session数据立刻存储值缓存。默认为false，表示在当前请求结束时才更新cache**/
        private boolean sessionToCacheImmediately = false;

        public String getUrlPatterns() {
            return urlPatterns;
        }

        public void setUrlPatterns(String urlPatterns) {
            this.urlPatterns = urlPatterns;
        }

        public String getCookieSidName() {
            return cookieSidName;
        }

        public void setCookieSidName(String cookieSidName) {
            this.cookieSidName = cookieSidName;
        }

        public String getCookieDomain() {
            return cookieDomain;
        }

        public void setCookieDomain(String cookieDomain) {
            this.cookieDomain = cookieDomain;
        }

        public String getCookiePath() {
            return cookiePath;
        }

        public void setCookiePath(String cookiePath) {
            this.cookiePath = cookiePath;
        }

        public String getSidHeaderKey() {
            return sidHeaderKey;
        }

        public void setSidHeaderKey(String sidHeaderKey) {
            this.sidHeaderKey = sidHeaderKey;
        }

        public String getSessionSourceHeaderKey() {
            return sessionSourceHeaderKey;
        }

        public void setSessionSourceHeaderKey(String sessionSourceHeaderKey) {
            this.sessionSourceHeaderKey = sessionSourceHeaderKey;
        }

        public int getExpireTime() {
            return expireTime;
        }

        public void setExpireTime(int expireTime) {
            this.expireTime = expireTime;
        }

        public String getPassPathPrefix() {
            return passPathPrefix;
        }

        public void setPassPathPrefix(String passPathPrefix) {
            this.passPathPrefix = passPathPrefix;
        }

        public String getPassPathPostfix() {
            return passPathPostfix;
        }

        public void setPassPathPostfix(String passPathPostfix) {
            this.passPathPostfix = passPathPostfix;
        }

        public boolean isSessionToCacheImmediately() {
            return sessionToCacheImmediately;
        }

        public void setSessionToCacheImmediately(boolean sessionToCacheImmediately) {
            this.sessionToCacheImmediately = sessionToCacheImmediately;
        }
    }

    public String getLogPath() {
        return logPath;
    }

    public YhaoInfraWebProperties setLogPath(String logPath) {
        this.logPath = logPath;
        return this;
    }
}
